package com.hq.fxh.No4_XiaoXi.IM;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hq.fxh.No0_ChengQu.Login.LoginActivity;
import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.Tool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.content.MessageContent;
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.enums.ContentType;
import cn.jpush.im.android.api.enums.MessageDirect;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.model.UserInfo;

import static cn.jpush.im.android.api.model.UserInfo.Field.nickname;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/10/12
 * @Email xlwenhao1858@sina.com
 */


public class Ladapte_LiaoTian extends BaseAdapter {
    List<Message> mData;

    public void setmData(List<Message> mData) {
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;

        if (getItemViewType(position) == 1) {
            MyViewHolder viewHolder = null;
            if (view == null || !(view.getTag() instanceof MyViewHolder)) {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_liaotian_my, null);
                viewHolder = new MyViewHolder(view);
                view.setTag(viewHolder);
            } else {
                viewHolder = (MyViewHolder) view.getTag();
            }

            Message message = mData.get(position);
            TextContent content = (TextContent) message.getContent();
            String text = content.getText();
            UserInfo targetInfo =   message.getFromUser();
            String address = targetInfo.getAddress();
            if (targetInfo == null) {
                Tool.setUrlToHeadImage(parent.getContext(), AppConstant.headimageUrl, viewHolder.imageItemHeadpic);

            } else {
                Tool.setUrlToHeadImage(parent.getContext(), address, viewHolder.imageItemHeadpic);

            }

            viewHolder.tvItemTextMessage.setText(text);

        } else {
            YouViewHolder viewHolder = null;
            if (view == null || !(view.getTag() instanceof YouViewHolder)) {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_liaotian_you, null);
                viewHolder = new YouViewHolder(view);
                view.setTag(viewHolder);
            } else {
                viewHolder = (YouViewHolder) view.getTag();
            }

            Message message = mData.get(position);
            UserInfo targetInfo = (UserInfo) message.getFromUser();
            String address = targetInfo.getAddress();
            if (targetInfo == null) {
                Tool.setUrlToHeadImage(parent.getContext(), AppConstant.headimageUrl, viewHolder.imageItemHeadpic);

            } else {
                Tool.setUrlToHeadImage(parent.getContext(), address, viewHolder.imageItemHeadpic);

            }
            TextContent content = (TextContent) message.getContent();
            String text = content.getText();
            viewHolder.tvItemTextMessage.setText(text);
        }


        return view;
    }


    @Override
    public int getItemViewType(int position) {

        Message message = mData.get(position);
        MessageDirect direct = message.getDirect();
        String name = direct.name();
        LogUtils.e(" type:" + name);
        if (name.equals("send")) {
            return 1;
        } else {
            return 2;
        }

    }

    static class MyViewHolder {
        protected TextView tvItemTextMessage;
        protected ImageView imageItemHeadpic;

        MyViewHolder(View rootView) {
            initView(rootView);
        }

        private void initView(View rootView) {
            tvItemTextMessage = (TextView) rootView.findViewById(R.id.tv_item_textMessage);
            imageItemHeadpic = (ImageView) rootView.findViewById(R.id.image_item_headpic);
        }
    }


    static class YouViewHolder {
        protected TextView tvItemTextMessage;
        protected ImageView imageItemHeadpic;

        YouViewHolder(View rootView) {
            initView(rootView);
        }

        private void initView(View rootView) {
            tvItemTextMessage = (TextView) rootView.findViewById(R.id.tv_item_textMessage);
            imageItemHeadpic = (ImageView) rootView.findViewById(R.id.image_item_headpic);
        }
    }
}
